package com.zsp.coupon.filter;


import com.google.common.util.concurrent.RateLimiter;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;


/**
 * 限流过滤器
 */
@Component
@SuppressWarnings("all")
public class RateLimiterFilter extends AbstactZuulFilter{

//    每秒可以获取到两个令牌
RateLimiter rateLimiter = RateLimiter.create(2.0);
    @Override
    protected Object cRun() {
        HttpServletRequest request=content.getRequest();
        if (rateLimiter.tryAcquire()){
            System.out.println("get rate token success");
        }else {
            System.out.println("ralte limit "+request.getRequestURI());
            return fail(402,"error rate limit");
        }
        return null;
    }

    @Override
    public String filterType() {
        return null;
    }

    @Override
    public int filterOrder() {
        return 2;
    }
}
